package com.xique.door.service.impl;


import com.xique.door.bean.domain.AccessDevice;
import com.xique.door.bean.domain.PermissionGroupDevice;
import com.xique.door.bean.domain.UserDevice;
import com.xique.door.service.IAccessDeviceDomainService;
import com.xique.door.service.IAccessDeviceService;
import com.xique.door.service.IPermissionGroupDeviceService;
import com.xique.door.service.IUserDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author shd
 * @create 2021/7/6 9:55
 */
@Service
public class AccessDeviceDomainServiceImpl implements IAccessDeviceDomainService {

    @Autowired
    private IAccessDeviceService accessDeviceService;

    @Autowired
    private IPermissionGroupDeviceService permissionGroupDeviceService;

    @Autowired
    private IUserDeviceService userDeviceService;

    /**
     * 添加设备
     * @author shd
     * @date 2021/7/6 10:04
     * @param accessDevice 设备信息
     * @param devices 权限组设备信息
     * @param userDevices 用户设备信息
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean addDevice(AccessDevice accessDevice, List<PermissionGroupDevice> devices, List<UserDevice> userDevices) {

        int count = accessDeviceService.insertAccessDevice(accessDevice);
        if (count <= 0) {
            return false;
        }

        devices.forEach(item -> {
            item.setDeviceId(accessDevice.getId());
        });
        count = permissionGroupDeviceService.batchInsertPermissionGroupDevice(devices);
        if (count <= 0) {
            return false;
        }

        userDevices.forEach(item -> {
            item.setDeviceId(accessDevice.getId());
        });
        count = userDeviceService.batchInsertUserDevice(userDevices);
        if (count <= 0) {
            return false;
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean editDevice(AccessDevice accessDevice, List<PermissionGroupDevice> addDevice,
                              List<Long> deleteIds, List<UserDevice> addUserDevice, List<Long> deleteUserDeviceIds) {

        int count = accessDeviceService.updateAccessDevice(accessDevice);
        if (count <= 0) {
            return false;
        }

        count = permissionGroupDeviceService.batchInsertPermissionGroupDevice(addDevice);
        if (count <= 0) {
            return false;
        }

        count = permissionGroupDeviceService.batchDeletePermissionGroupDevice(deleteIds);
        if (count <= 0) {
            return false;
        }

        count = userDeviceService.batchInsertUserDevice(addUserDevice);
        if (count <= 0) {
            return false;
        }

        count = userDeviceService.batchDeleteUserDevice(deleteUserDeviceIds);
        if (count <= 0) {
            return false;
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean removeDevice(List<Long> deviceIds, List<Long> groupDeviceIds, List<Long> userDeviceIds) {

        int count = accessDeviceService.deleteAccessDeviceByIds(deviceIds);
        if (count <= 0) {
            return false;
        }

        count = permissionGroupDeviceService.batchDeletePermissionGroupDevice(groupDeviceIds);
        if (count <= 0) {
            return false;
        }

        count = userDeviceService.batchDeleteUserDevice(userDeviceIds);
        if (count <= 0) {
            return false;
        }

        return true;
    }
}
